#include "Clustering.h"
#include <iostream>
namespace AAM{
    bool compImg(pair<double,int> a, pair<double,int> b){
        return a.first<b.first;
    }
double PhotoClustering::getOutlierThreshold(vector<pair<double,int> > vec){
    sort(vec.begin(),vec.end(),compImg);
    //Search for the outlier
    int q1pos = vec.size()/4;
    int q3pos = q1pos*3;
    double q1 = vec.at(q1pos).first;
    double q3 = vec.at(q3pos).first;
    double iqr = q3-q1;
    double outlierThreshold = q1-(1.5*iqr);
    return outlierThreshold;
}
vector<Event> PhotoClustering::searchForEvents(vector<pair<double,int> > &vec, int beginning){
    
    double outlierThreshold = getOutlierThreshold(vec);
//    double mean = vectorMean(vec);
    vector<Event> events;
    
    vector<int> position;
//    double tshold = mean*(1+perc);
    for(int a=0;a<vec.size();a++){
        if(vec[a].first<outlierThreshold){
            position.push_back(a);
        }
    }
    if(position.size()!=0){
        int lastStart = beginning;
        for(int a=0;a<position.size()-1;a++){
//            Event e(vec[lastStart].second,vec[position[a]].second);
            Event e(lastStart,vec[position[a]].second-1);
            std::cout << std::endl << "{" << lastStart << " , " << vec[position[a]].second-1 << "}";
            events.push_back(e);
            lastStart = vec[position[a]].second;
        }
        Event e(lastStart,vec.size());
        std::cout << std::endl << "{" << lastStart << " , " << vec.size() << "}";
        events.push_back(e);
    }
    return events;
}
}